#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "our_grep.h"

int main(int argc,char **argv)
{
  int res;

  if(argc < 3)
    {
      if(argv[1] == "--help")
	{
	printf("help a venir\n");
	return 1;
	}
      else
	{
	  printf("Nombre paramètres insuffisant\n");
	  return 1;
	}
    }
  if(argc >= 3)
    {
      res = verifoption(argv[1]);
      if(res == 1)
	grep_whith_option(argv[1],argv[2],argv[3]);
      if(res == 2)
	printf("help\n");
      if(res == 0)
	grep_whithout_option(argv[1],argv[2]);
    }
  return 0;
}

int verifoption(char *str)
{
  int i;
  
  i = 0;
  if(str[i] == '-' && str[i+1] == 'i')
    return 1;
  if(str[i] == '-' && str[i+1] == 'b')
    return 2;
  else
    return 0;
}

void grep_whith_option(char *option, char *str, char *file)
{
  printf("grepwhithoption\n");
}

void grep_whithout_option(char *str,char *file)
{
  FILE *fic;
  char ligne[512];
  int i;

  fic = fopen(file, "r");
  if(fic == NULL)
    {
      printf("bad file %s\n", file);
      exit(1);
    }
  i = 0;
  while(fgets(ligne, 512,fic) != NULL)
    {
      if(strstr(ligne,str))
	{
	  printf("%s\n",ligne);
	  exit(1);
	}
      i++;
    }
}

